package com.jiufen.Service.impl;


import cn.hutool.core.lang.Console;
import com.jiufen.Exception.RemainingStudentsException;
import com.jiufen.Mapper.DeptMapper;
import com.jiufen.Pojo.Dept;
import com.jiufen.Service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> queryDeptAll() {
        List<Dept> depts = deptMapper.queryDeptAll();
        return depts;
    }

    @Override
    public Integer deleteById(Integer id) throws RemainingStudentsException {
        //删除部门前需要统计员工人数
        Integer numberPeople = deptMapper.queryNumberPeople(id);
        log.debug("该部门下员工人数:{}",numberPeople);
        if (numberPeople > 0 ){
            throw new RemainingStudentsException("该部门下还有 " + numberPeople + " 名员工未处理，无法删除部门");
        }
        //确保该部门无员工才可以删除
        Integer i = deptMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        Console.log("Service-addDept= "+dept);
        Integer i = deptMapper.addDept(dept);
        return i;
    }

    @Override
    public Dept queryByDeptId(Integer id) {
        Dept dept  = deptMapper.queryByDeptId(id);
        return dept;
    }

    @Override
    public Integer updateDeptById(Dept dept) {
        Console.log("Service-updateDeptById= "+dept);
        dept.setUpdateTime(LocalDateTime.now());
        Integer i = deptMapper.updateDeptById(dept);
        return i;
    }
}
